﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.talent.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.talent.dao.*;
import com.hh.bss.talent.entity.*;
import com.hh.bss.talent.service.*;
import com.hh.bss.talent.vo.query.*;
import com.hh.bss.util.*;



import static com.hh.bss.util.ObjectUtils.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

@Repository
public class CvContactDetailDaoImpl extends BaseHibernateDao<CvContactDetail, java.lang.Integer> implements CvContactDetailDao<CvContactDetail, java.lang.Integer> {

	public Class getEntityClass() {
		return CvContactDetail.class;
	}

	public Page findPage(CvContactDetailQuery query) {
		// XsqlBuilder syntax,please see
		// http://code.google.com/p/rapid-xsqlbuilder
		// [column]为字符串拼接, {column}为使用占位符.
		// [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接
		// [column] 为PageRequest的属性

		Class[] os = { CvContactDetailQuery.class };
		Class property = org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
		if (property != null) {
			if ("java.lang.String".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), query.getQuery());

			} else if ("java.lang.Integer".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));

			} else if ("java.lang.Double".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
			} else if ("java.util.Date".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
			}
		}
		// 生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
		StringBuilder sql2 = new StringBuilder("select t from CvContactDetail t where 1=1 ");
		if (isNotEmpty(query.getId())) {
			sql2.append(" and  t.id = :id ");
		}
		if (isNotEmpty(query.getCvId())) {
			sql2.append(" and  t.cvId = :cvId ");
		}
		if (isNotEmpty(query.getContactType())) {
			sql2.append(" and  t.contactType like :contactType ");
			query.setContactType("%" + query.getContactType() + "%");

		}
		if (isNotEmpty(query.getNbr())) {
			sql2.append(" and  t.nbr like :nbr ");
			query.setNbr("%" + query.getNbr() + "%");

		}
		if (isNotEmpty(query.getNbrExt1())) {
			sql2.append(" and  t.nbrExt1 like :nbrExt1 ");
			query.setNbrExt1("%" + query.getNbrExt1() + "%");

		}
		if (isNotEmpty(query.getNbrExt2())) {
			sql2.append(" and  t.nbrExt2 like :nbrExt2 ");
			query.setNbrExt2("%" + query.getNbrExt2() + "%");

		}
		if (isNotEmpty(query.getComment())) {
			sql2.append(" and  t.comment like :comment ");
			query.setComment("%" + query.getComment() + "%");

		}
		if (isNotEmpty(query.getCreator())) {
			sql2.append(" and  t.creator like :creator ");
			query.setCreator("%" + query.getCreator() + "%");

		}
		if (isNotEmpty(query.getCreateDtBegin())) {
			sql2.append(" and  t.createDt >= :createDtBegin ");
		}
		if (isNotEmpty(query.getCreateDtEnd())) {
			sql2.append(" and  t.createDt <= :createDtEnd ");
		}
		if (isNotEmpty(query.getModifier())) {
			sql2.append(" and  t.modifier like :modifier ");
			query.setModifier("%" + query.getModifier() + "%");

		}
		if (isNotEmpty(query.getModifiedDtBegin())) {
			sql2.append(" and  t.modifiedDt >= :modifiedDtBegin ");
		}
		if (isNotEmpty(query.getModifiedDtEnd())) {
			sql2.append(" and  t.modifiedDt <= :modifiedDtEnd ");
		}
		if (isNotEmpty(query.getSortColumns())) {
			sql2.append(" order by :sortColumns ");
		}

		return pageQuery(sql2.toString(), query);
	}

	public void flush() {

		super.flush();
	}

	@Override
	public void removeByCvId(Integer cvId) {
		if (cvId != null && cvId > 0) {
			this.getJdbcDao().getJdbcTemplate().update("update cv_contact_detail a set a.is_delete = 1 where a.cv_id=?", cvId);
			// execute("update cv_contact_detail a set a.is_delete = 1 where a.cv_id = ?",cvId);
		}

	}

	@Override
	public String getCvIds(String cvId) {
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append("select group_concat(distinct a.cv_id) cvIds from cv_contact_detail a, ( select a1.nbr from cv_contact_detail a1 where a1.is_delete = 0 and a1.nbr <> '' and a1.cv_id = ?  ) b where a.is_delete = 0 and a.cv_id <> ? and a.nbr = b.nbr");
		values.add(cvId);
		values.add(cvId);
		List<Map> l = this.getJdbcDao().findByQuery(sql.toString(), values.toArray(), new MapObjectRowMapper());
		if (l != null && l.size() > 0) {
			Map<String, String> m = l.get(0);
			String cvIds = m.get("cvIds");
			if (StringUtils.isNotEmpty(cvIds)) {
				return cvIds;
			}
		}
		return null;
	}

	@Override
	public void recoverByCvId(String cvIds) {
		if (cvIds != null && cvIds.trim().length() > 0) {
			this.getJdbcDao().getJdbcTemplate().update("update cv_contact_detail a set a.is_delete = 0 where find_in_set(a.cv_id,?)", cvIds);
		}
	}

}
